var http = require('http');
var querystring = require('querystring');

var allmess={
    CompanyId:'0010',
    Source:'wyc1',
    IPCType:'positionVehicle',
    positionVehicle:
        [
            { "CompanyId":"0011","VehicleNo":"6001","VehicleRegionCode":"370030","PositionTime":"1523215478",
                "Longitude":"118.123","Latitude":"36.123","Speed":"30","Direction":"80","Elevation":"30","Mileage":"","Encrypt":"",
                "WarnStatus":"","VehStatus":"","BizStatus":"","OrderId":"003"
            },
            { "CompanyId":"0011","VehicleNo":"6002","VehicleRegionCode":"370031","PositionTime":"1524321478","Longitude":"118.234",
                "Latitude":"36.234","Speed":"29","Direction":"81","Elevation":"31","Mileage":"","Encrypt":"","WarnStatus":"","VehStatus":"","BizStatus":"","OrderId":"003"},

        ]

};

for(var j=0;j<3;j++)
{
    var item={ "CompanyId":"0011","VehicleNo":"6002"+j,"VehicleRegionCode":"370031","PositionTime":"1524321478","Longitude":"118.234",        "Latitude":"36.234","Speed":"29","Direction":"81","Elevation":"31","Mileage":"","Encrypt":"","WarnStatus":"","VehStatus":"","BizStatus":"","OrderId":"003"};
    allmess["positionVehicle"].push(item);
}



var http = require('http');
var ProtoBuf = require("protobufjs");

// 同步创建proto，异步用ProtoBuf.load("./proto/a.proto",function(err,root){...})
var root = ProtoBuf.loadSync("./wycproto/company.proto");
// 获得一个包类型
var textMessage = root.lookupType("wyc.positionVehicleMess");



// 检测是否符合类型
var errMsg = textMessage.verify(allmess);
if (errMsg) throw Error(errMsg);

// 生成一条message
var message = textMessage.create(allmess); // or use .fromObject if conversion is necessary

// 将message转成buffer,node下转成Buffer,browser下转成Uint8Array
var buffer = textMessage.encode(message).finish();
console.log(buffer);



var options = {
    host:'localhost',
    path:'/position/vehicle',
    port:8081,
    method:'POST',
    headers:{
        'Content-Type':'application/x-protobuf',
        'Content-Length':buffer.length
    }
}

var req = http.request(options, function(res){
    res.setEncoding('utf8');
    res.on('data',function(data){
        console.log("data:",data);   //一段html代码
    });
});

req.on("timeout", function() {
    console.log("timeout received");
    if (req.res) {
        req.res.emit("abort");
    }

    req.abort();
});


req.write(buffer);
req.end;